package bid.util;

import java.sql.*;

/**
 * @author oldliu
 * @since 1.0
 */
//数据库连接工具类
public class DBUtil {
    private DBUtil() {
    }

    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection get() {
        String url = "jdbc:mysql://localhost:3308/fuyang?autoReconnect=true&useUnicode=true" +
                "&characterEncoding=utf8" +
                "&serverTimezone=GMT%2B8";
        String user = "root";
        String pass = "root1234";
        Connection con = null;
        try {
            con = DriverManager.getConnection(url, user, pass);
            return con;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public static void close(PreparedStatement ps, Connection con) {

        close(ps);
        close(con);
    }

    public static void close(ResultSet rs, PreparedStatement ps, Connection con) {
        close(rs);
        close(ps);
        close(con);
    }

    public static void close(ResultSet rs) {
        try {
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void close(PreparedStatement rs) {
        try {
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void close(Connection rs) {
        try {
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
